package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.ProductCart;
import org.dromara.shopping.service.IProductCartService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 购物车
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/product/cart")
public class ProductCartController extends BaseController {

    private final IProductCartService productCartService;

    /**
     * 查询购物车列表
     */
    @SaCheckPermission("shopping:cart:list")
    @GetMapping("/list")
    public TableDataInfo<ProductCart> list(ProductCart productCart, PageQuery pageQuery) {
        QueryWrapper<ProductCart> qw = new QueryWrapper<>();

        TableDataInfo<ProductCart> td = PageUtils.buildDataInfo(productCartService.page(PageUtils.buildPage(),qw));
        List<ProductCart> list = td.getRows();
        return td;
    }

    /**
     * 导出购物车列表
     */
    @SaCheckPermission("shopping:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductCart productCart, HttpServletResponse response) {
        List<ProductCart> list = productCartService.list();
        ExcelUtil.exportExcel(list, "购物车", ProductCart.class, response);
    }

    /**
     * 获取购物车详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:cart:query")
    @GetMapping("/{id}")
    public R<ProductCart> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(productCartService.getById(id));
    }

    /**
     * 新增购物车
     */
    @SaCheckPermission("shopping:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductCart productCart) {
        return toAjax(productCartService.save(productCart));
    }

    /**
     * 修改购物车
     */
    @SaCheckPermission("shopping:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCart productCart) {
        return toAjax(productCartService.updateById(productCart));
    }

    /**
     * 删除购物车
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productCartService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
